import React, {useEffect} from 'react'
import router from 'umi/router'
import { connect } from 'dva'
import MessageModel from 'components/common/MessageModel'
import { checkDel, checkStop, checkOpen } from 'dic/messageModel'

function GetActions({ dispatch, t , handleRest}) {
    useEffect(() => {
        dispatch({
            type: 'deviceCheck/cleanDetail',
        })
    },[dispatch])
    // 跳转点检详情页
    const toDetail = (value) => {
        router.push({
            pathname:'/deviceCheck/detail',
            query: {
                id: t.id,
                type: value,
            },
        });
    }
  
    // 编辑
    const editor = () => {
        if (t.status === 5 || t.status === 1 ||t.status === 4 ||t.status === 2) {
            router.push({
                pathname:'/deviceCheck/edit',
                query: {
                    id: t.id,
                    type: 'editor'
                },
            });
        }
    }
    
    // 启用 或 停止
    const stopAddStart = () => {
        dispatch({
            type: 'deviceCheck/getCheckStatus',
            payload: {
                id: t.id,
                status: t.status === 3 ? 4 : 3,
                searchData: {
                    size: 10,
                    current: 1
                }
            }
        })
        handleRest()
    }

    // 删除
    const deletList = () => {
        dispatch({
            type: 'deviceCheck/deleteCheck',
            payload: {
                id: t.id,
                searchData: {
                    size: 10,
                    current: 1
                }
            }
        })
        handleRest()
    }
    // const user = localStorage.getItem('loginInfo') && JSON.parse(localStorage.getItem('loginInfo')).name

    const gitFirstAction = (key, userNameIsEqual) => {
        switch(key) {
            case 1:
                return (
                    <>
                    {
                        userNameIsEqual
                        ?
                        <span className='btnStyl' onClick={() => toDetail('aduit')} style={{ marginRight: '0.16rem' }}>审批</span>
                        :
                        null
                    }
                    </>
                );
            case 2:
                return null
            case 3:
                return (
                    <MessageModel data={checkStop} handleClick={stopAddStart}/>
                );
            case 4:
                return (
                    <MessageModel data={checkOpen} handleClick={stopAddStart}/>
                );
            case 5:
                return null
            default:
                return;
        }
    }
   
    return(
        <span>
            {gitFirstAction(t.status, t.userNameIsEqual)}
            {
                t.status === 3
                ?
                null
                :
                <>
                    <span
                    className='btnStyl'
                    onClick={editor} 
                    style={{ marginRight: '0.16rem'}}
                    >编辑</span>
                    <MessageModel 
                    data={checkDel} 
                    handleClick={deletList} 
                    />
                </>
            }
            <span className='btnStyl' onClick={() => toDetail()}>查看详情</span>
        </span>
    )
}


export default connect(({ deviceRepair }) => ({
    deviceRepair
}))(GetActions)